package com.study.hadoop.weather;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * 分组
 * 按照年月分组
 * @author stephen
 * @create 2019-01-10 21:50
 * @since 1.0.0
 */
public class WeatherGroupComparator extends WritableComparator {

    protected WeatherGroupComparator() {
        super(Weather.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        Weather w1 = (Weather) a;
        Weather w2 = (Weather) b;

        int y = Integer.compare(w1.getYear(), w2.getYear());
        if (y == 0) {
            return Integer.compare(w1.getMonth(), w2.getMonth());
        }
        return y;
    }
}
